package com.hb.game.pk.ui.activity.mine.info;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hb.game.pk.APP;
import com.hb.game.pk.R;
import com.hb.game.pk.adpter.ChooseHeadAdpter;
import com.hb.game.pk.api.Host;
import com.hb.game.pk.api.RedBag;
import com.hb.game.pk.base.BaseActivity;
import com.hb.game.pk.beans.NullBean;
import com.hb.game.pk.event.ChangeHeadEvent;
import com.hb.game.pk.network.RetrofitCreateHelper;
import com.hb.game.pk.utils.SpUtils;
import com.hb.game.pk.utils.ToastUtils;
import com.hb.game.pk.widgets.titlebar.TitleBar;
import com.google.gson.Gson;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import butterknife.BindView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class ChooseHeadActivity extends BaseActivity<NullBean> {
    @BindView(R.id.title_bar)
    TitleBar titleBar;
    @BindView(R.id.rv_list)
    RecyclerView rv_list;
    private List<Integer> resource = APP.headResource;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_choose_head;
    }

    @Override
    protected int getRootViewId() {
        return R.id.layout_root;
    }

    @Override
    protected int getRefreshId() {
        return 0;
    }

    @Override
    protected int getListViewId() {
        return 0;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        titleBar.getLlLeft().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ChooseHeadActivity.this.finish();
            }
        });
        rv_list.setLayoutManager(new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL));
        mAdapter = new ChooseHeadAdpter(R.layout.item_choose_hed_list, this);
        mAdapter.setNewData(resource);
        rv_list.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, final int position) {
                call = RetrofitCreateHelper.createApi(RedBag.class, Host.HOST).changeHead("avatar/" + (position + 1) + ".jpg");
                call.enqueue(new Callback<NullBean>() {
                    @Override
                    public void onResponse(Call<NullBean> call, Response<NullBean> response) {
                        NullBean body = response.body();
                        if (body == null) {
                            ToastUtils.showToast("请求错误");
                            return;
                        }
                        if (body.getStatus() != 1) {
                            ToastUtils.showToast(body.getInfo());
                            return;
                        }
                        ChangeHeadEvent changeHeadEvent = new ChangeHeadEvent();
                        changeHeadEvent.res = APP.headResource.get(position);
                        EventBus.getDefault().post(changeHeadEvent);
                        APP.userInfo.setFace("avatar/" + (position + 1) + ".jpg");
                        SpUtils.putString(ChooseHeadActivity.this, "userInfo", new Gson().toJson( APP.userInfo));
                        ChooseHeadActivity.this.finish();
                    }

                    @Override
                    public void onFailure(Call<NullBean> call, Throwable t) {
                        ToastUtils.showToast("网络请求错误");
                    }
                });
            }
        });
    }
}
